<?php
include_once '../utils/WCDBLayer.php';
include_once '../comic/WCPersistantObject.php';
include_once '../utils/WCRelation.php';
include_once '../comic/WCAuthor.php';
include_once '../comic/WCComic.php';


function configure(){
	echo '<p class="success">Starting...</p>';
	$db = new WCDBLayer(Conf::$db_url,Conf::$db_user,Conf::$db_password);
	try{
		$db->connect(Conf::$db);
	}catch(WCException $e){
		echo '<p class="error">Error ',$e->getCode()+': ',$e->getMessage(),' encountered while attempting to connect to the database.  Please check connection settings set in Conf.php and try again.</p>';
		echo $e->printHTMLTrace();
		exit;
	}
	echo '<p class="success">Successfully connected to database.</p>';
	
	try{
		$wcr = new WCRelation($db);
		$wcr->createTable(isset($_GET['delete_wc_relation']));
		echo '<p class="success">Successfully created table wc_relation.</p>';
	}catch(WCException $e){
		if($e->getCode() == WCException::$DB_QUERY_ERROR && $e->getMessage() == 'Table already exists.'){
			echo '<p class="warning">Table wc_relation already exists, to drop and recreate click <a href="?delete_wc_relation">here</a>.';
		}else{
			echo '<p class="error">Error ',$e->getCode()+': ',$e->getMessage(),' encountered while attempting to create table wc_relation.</p>';
			echo $e->printHTMLTrace();
			exit;
		}
	}
	
	try{
		$wca = new WCAuthor($db);
		$wca->createTable(isset($_GET['delete_wc_author']));
		echo '<p class="success">Successfully created table wc_author.</p>';
	}catch(WCException $e){
		if($e->getCode() == WCException::$DB_QUERY_ERROR && $e->getMessage() == 'Table already exists.'){
			echo '<p class="warning">Table wc_author already exists, to drop and recreate click <a href="?delete_wc_author">here</a>.';
		}else{
			echo '<p class="error">Error ',$e->getCode()+': ',$e->getMessage(),' encountered while attempting to create table wc_author.</p>';
			echo $e->printHTMLTrace();
			exit;
		}
	}
	
	try{
		$wcc = new WCComic($db);
		$wcc->createTable(isset($_GET['delete_wc_comic']));
		echo '<p class="success">Successfully created table wc_comic.</p>';
	}catch(WCException $e){
		if($e->getCode() == WCException::$DB_QUERY_ERROR && $e->getMessage() == 'Table already exists.'){
			echo '<p class="warning">Table wc_comic already exists, to drop and recreate click <a href="?delete_wc_comic">here</a>.';
		}else{
			echo '<p class="error">Error ',$e->getCode()+': ',$e->getMessage(),' encountered while attempting to create table wc_comic.</p>';
			echo $e->printHTMLTrace();
			exit;
		}
	}
}
?>
<html>
<head>
	<title>Configure Daily WebComics</title>
	<style type="text/css">
	.success{
		color:green;
		}
	.error{
		color:red;
		}
	.warning{
		color:maroon;
		}
	</style>
</head>
<body>
<?php configure(); ?>
</body>
</html>